package com.angel.web.app;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.angel.core.base.JsonEntity;
import com.angel.core.util.FileUpload;
import com.angel.core.util.JsonUtil;
import com.angel.health.BuzConsult;
import com.angel.health.BuzDoctor;
import com.angel.provider.app.BuzConsultProvider;

/**
 * @author 作者姓名 cheng
 * @version 创建时间：2016年9月22日 下午6:13:33 类说明 我的咨询
 */
@Controller
@RequestMapping("app/consult")
public class AppConsultController {
	@Autowired
	private BuzConsultProvider buzConsultProvider;
	
	/**
	 * 添加咨询记录
	 * @param buzConsult
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "addConsult", produces = "application/json; charset=UTF-8")
	public String addConsult(BuzConsult buzConsult, MultipartHttpServletRequest request) {
		MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
		String uploadUrl;
		try {
			uploadUrl = FileUpload.uploadForFastdfs(multiFileMap);
			buzConsult.setImgUri(uploadUrl);
			int insert = buzConsultProvider.insert(buzConsult);
			if (insert > 0) {
				return JsonUtil.toJson(new JsonEntity("200", buzConsult));
			} else {
				return JsonUtil.toJson(new JsonEntity("-1", "失败"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return JsonUtil.toJson(new JsonEntity("-1", "失败"));
		}
	}
	
	/**
	 * 修改咨询信息（包括对咨询的评价，回答咨询等）
	 * @param buzConsult
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="answering", produces = "application/json; charset=UTF-8")
	public String answering(BuzConsult buzConsult){
		if(buzConsult.getId() == null){
			return JsonUtil.toJson(new JsonEntity("-1", "参数错误"));
		}
		buzConsult.setAnswerTime(new Date());
		int updateById = buzConsultProvider.updateById(buzConsult);
		if(updateById > 0){
			return JsonUtil.toJson(new JsonEntity("200", "成功"));
		}else{
			return JsonUtil.toJson(new JsonEntity("-1", "失败"));
		}
	}

	/**
	 * 查看咨询记录
	 * @param buzConsult
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "index", produces = "application/json; charset=UTF-8")
	public String index(BuzConsult buzConsult) {
		if(buzConsult.getCustomerId() == null && buzConsult.getDoctorId() == null){
			return JsonUtil.toJson(new JsonEntity("-1", "参数不正确！"));
		}
		List<Map<String,Object>> list = buzConsultProvider.findAll(buzConsult);
		return JsonUtil.toJson(new JsonEntity("200", list));
	}
	
	/**
	 * 查询指定医师的用户评价
	 * @param doctor
	 * @return
	 */
//	@ResponseBody
//	@RequestMapping(value = "findDoctorConsults", produces = "application/json; charset=utf-8")
//	public String findDoctorConsults(BuzDoctor doctor, HttpServletRequest req,@ModelAttribute("checkCode")String checkCode){
//		// 获取医师是否认真信息，200为认证201为未认证
//		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
//		if (StringUtil.isEmpty(checkCode)) {
//			return JsonUtil.toJson(new JsonEntity("-1", "参数不正确！"));
//		}
//		if ("200".equals(checkCode) && doctor.getId() != null) {
//			SysUser su = new SysUser();
//			su.setId(doctor.getId());
//			su = sysUserProvider.findById(su);
//			if(su == null){
//				return JsonUtil.toJson(new JsonEntity("-1", "参数不正确！"));
//			}
//			doctor.setId(null);
//			doctor.setMobile(su.getMobile());
//		}
//		list = provider.findDoctorAll(doctor);
//		if(list != null && list.size() > 0){
//			Integer doctorId = (Integer) list.get(0).get("id");
//			BuzConsult consult = new BuzConsult();
//			consult.setDoctorId(doctorId);
//			resultList = buzConsultProvider.findAll(consult);
//		}
//
//		return JsonUtil.toJson(new JsonEntity("200", resultList));
//	}
	
	/**
	 * 分配咨询用户给医师
	 * @param doctor
	 * @return
	 * */
	@ResponseBody
	@RequestMapping(value = "allotCustToDoc", produces = "application/json; charset=utf-8")
	public String allotCustToDoc(BuzDoctor doctor){
		BuzConsult consult = new BuzConsult();
		consult.setQuestion(doctor.getGoodAt());
		List<Map<String, Object>> list = buzConsultProvider.allotCustToDoc(consult);
		return JsonUtil.toJson(new JsonEntity("200", list));
	}
	
	//查询医师指定用户的评价
	@ResponseBody
	@RequestMapping(value = "findDoctorCustomerConsults", produces = "application/json; charset=utf-8")
	public String findDoctorCustomerConsults(BuzConsult consult, HttpServletRequest req,@ModelAttribute("checkCode")String checkCode){
		if(consult.getCustomerId() == null){
			return JsonUtil.toJson(new JsonEntity("-1", "请选择会员"));
		}
		if(consult.getDoctorId() == null){
			return JsonUtil.toJson(new JsonEntity("-1", "请选择医师"));
		}
		List<Map<String, Object>> list = buzConsultProvider.findAll(consult);
		return JsonUtil.toJson(new JsonEntity("200", list));
	}
	
	//查看医师评价
	@ResponseBody
	@RequestMapping(value = "findDoctorConsultAtten", produces = "application/json; charset=utf-8")
	public String findDoctorConsultAtten(BuzConsult consult){
		List<Map<String, Object>> list = buzConsultProvider.findDoctorConsultAtten(consult);
		return JsonUtil.toJson(new JsonEntity("200", list));
	}
}
